#include "ukuidebugconsole.h"

#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include <qnamespace.h>
namespace ukuiwaylandclient
{

UkuiDebugConsole::UkuiDebugConsole(UkuiWindowManager *windowmanager)
    : QWidget(nullptr)
    , m_treeWidget(new QTreeWidget(this))
{
    connect(windowmanager, &UkuiWindowManager::windowAdded, this, &UkuiDebugConsole::handleWindowAdded);
    connect(windowmanager, &UkuiWindowManager::windowRemoved, this, &UkuiDebugConsole::handleWindowRemoved);
    m_treeWidget->setColumnCount(2);
    QStringList headers;
    headers << "属性"
            << "值";
    m_treeWidget->setHeaderLabels(headers);
    m_treeWidget->setColumnWidth(0, QFontMetrics(m_treeWidget->font()).horizontalAdvance("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") * 1.5);
    m_treeWidget->setAlternatingRowColors(true);
    m_treeWidget->setIndentation(16);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_treeWidget);
    setLayout(layout);

    setWindowFlags(Qt::WindowStaysOnTopHint);

    windowmanager->start();
}

UkuiDebugConsole::~UkuiDebugConsole()
{
}

void UkuiDebugConsole::handleWindowAdded(UkuiWindow *window)
{
    QTreeWidgetItem *item = new QTreeWidgetItem(m_treeWidget);
    item->setText(0, window->uuid());
    item->setText(1, window->title());
    connect(window, &UkuiWindow::titleChanged, this, [item](const QString &title) {
        item->setText(1, title);
    });

    m_treeWidget->addTopLevelItem(item);

    QTreeWidgetItem *appIdItem = new QTreeWidgetItem(item);
    appIdItem->setText(0, "AppId");
    appIdItem->setText(1, window->appId());
    item->addChild(appIdItem);
    connect(window, &UkuiWindow::appIdChanged, this, [appIdItem](const QString &appId) {
        appIdItem->setText(1, appId);
    });

    QTreeWidgetItem *pidItem = new QTreeWidgetItem(item);
    pidItem->setText(0, "PID");
    pidItem->setText(1, QString::number(window->pid()));
    item->addChild(pidItem);
    connect(window, &UkuiWindow::pidChanged, this, [pidItem](int pid) {
        pidItem->setText(1, QString::number(pid));
    });

    QTreeWidgetItem *geometryItem = new QTreeWidgetItem(item);
    geometryItem->setText(0, "Geometry");
    geometryItem->setText(1, QString("%1, %2, %3 x %4").arg(window->geometry().x()).arg(window->geometry().y()).arg(window->geometry().width()).arg(window->geometry().height()));
    item->addChild(geometryItem);
    connect(window, &UkuiWindow::geometryChanged, this, [geometryItem](const QRect &geometry) {
        geometryItem->setText(1, QString("%1, %2, %3 x %4").arg(geometry.x()).arg(geometry.y()).arg(geometry.width()).arg(geometry.height()));
    });

    QTreeWidgetItem *themedIconNameItem = new QTreeWidgetItem(item);
    themedIconNameItem->setText(0, "ThemedIconName");
    themedIconNameItem->setText(1, window->themedIconName());
    item->addChild(themedIconNameItem);
    connect(window, &UkuiWindow::themedIconNameChanged, this, [themedIconNameItem](const QString &name) {
        themedIconNameItem->setText(1, name);
    });

    QTreeWidgetItem *resourceNameItem = new QTreeWidgetItem(item);
    resourceNameItem->setText(0, "ResourceName");
    resourceNameItem->setText(1, window->resourceName());
    item->addChild(resourceNameItem);
    connect(window, &UkuiWindow::resourceNameChanged, this, [resourceNameItem](const QString &name) {
        resourceNameItem->setText(1, name);
    });

    QTreeWidgetItem *parentItem = new QTreeWidgetItem(item);
    parentItem->setText(0, "ParentWindow");
    parentItem->setText(1, window->parentWindow() ? window->parentWindow()->uuid() : "None");
    item->addChild(parentItem);
    connect(window, &UkuiWindow::parentWindowChanged, this, [parentItem, window](UkuiWindow *parent) {
        parentItem->setText(1, parent ? parent->uuid() : "None");
    });

    QTreeWidgetItem *virtualDesktopItem = new QTreeWidgetItem(item);
    virtualDesktopItem->setText(0, "VirtualDesktop");
    virtualDesktopItem->setText(1, window->virtualDesktop());
    item->addChild(virtualDesktopItem);
    connect(window, &UkuiWindow::virtualDesktopChanged, this, [virtualDesktopItem](const QString &virtualDesktopId) {
        virtualDesktopItem->setText(1, virtualDesktopId);
    });

    QTreeWidgetItem *minimizedItem = new QTreeWidgetItem(item);
    minimizedItem->setText(0, "Minimized");
    minimizedItem->setText(1, window->isMinimized() ? "✓" : "✗");
    item->addChild(minimizedItem);
    connect(window, &UkuiWindow::stateChanged, this, [minimizedItem, window](uint32_t state) {
        minimizedItem->setText(1, window->isMinimized() ? "✓" : "✗");
    });

    QTreeWidgetItem *maximizedItem = new QTreeWidgetItem(item);
    maximizedItem->setText(0, "Maximized");
    maximizedItem->setText(1, window->isMaximized() ? "✓" : "✗");
    item->addChild(maximizedItem);
    connect(window, &UkuiWindow::stateChanged, this, [maximizedItem, window](uint32_t state) {
        maximizedItem->setText(1, window->isMaximized() ? "✓" : "✗");
    });

    QTreeWidgetItem *fullScreenItem = new QTreeWidgetItem(item);
    fullScreenItem->setText(0, "FullScreen");
    fullScreenItem->setText(1, window->isFullScreen() ? "✓" : "✗");
    item->addChild(fullScreenItem);
    connect(window, &UkuiWindow::stateChanged, this, [fullScreenItem, window](uint32_t state) {
        fullScreenItem->setText(1, window->isFullScreen() ? "✓" : "✗");
    });

    QTreeWidgetItem *keepAboveItem = new QTreeWidgetItem(item);
    keepAboveItem->setText(0, "KeepAbove");
    keepAboveItem->setText(1, window->isKeepAbove() ? "✓" : "✗");
    item->addChild(keepAboveItem);
    connect(window, &UkuiWindow::stateChanged, this, [keepAboveItem, window](uint32_t state) {
        keepAboveItem->setText(1, window->isKeepAbove() ? "✓" : "✗");
    });

    QTreeWidgetItem *keepBelowItem = new QTreeWidgetItem(item);
    keepBelowItem->setText(0, "KeepBelow");
    keepBelowItem->setText(1, window->isKeepBelow() ? "✓" : "✗");
    item->addChild(keepBelowItem);
    connect(window, &UkuiWindow::stateChanged, this, [keepBelowItem, window](uint32_t state) {
        keepBelowItem->setText(1, window->isKeepBelow() ? "✓" : "✗");
    });

    QTreeWidgetItem *onAllDesktopsItem = new QTreeWidgetItem(item);
    onAllDesktopsItem->setText(0, "OnAllDesktops");
    onAllDesktopsItem->setText(1, window->isOnAllDesktops() ? "✓" : "✗");
    item->addChild(onAllDesktopsItem);
    connect(window, &UkuiWindow::stateChanged, this, [onAllDesktopsItem, window](uint32_t state) {
        onAllDesktopsItem->setText(1, window->isOnAllDesktops() ? "✓" : "✗");
    });

    QTreeWidgetItem *demandsAttentionItem = new QTreeWidgetItem(item);
    demandsAttentionItem->setText(0, "DemandsAttention");
    demandsAttentionItem->setText(1, window->isDemandsAttention() ? "✓" : "✗");
    item->addChild(demandsAttentionItem);
    connect(window, &UkuiWindow::stateChanged, this, [demandsAttentionItem, window](uint32_t state) {
        demandsAttentionItem->setText(1, window->isDemandsAttention() ? "✓" : "✗");
    });

    QTreeWidgetItem *closeableItem = new QTreeWidgetItem(item);
    closeableItem->setText(0, "Closeable");
    closeableItem->setText(1, window->isCloseable() ? "✓" : "✗");
    item->addChild(closeableItem);
    connect(window, &UkuiWindow::stateChanged, this, [closeableItem, window](uint32_t state) {
        closeableItem->setText(1, window->isCloseable() ? "✓" : "✗");
    });

    QTreeWidgetItem *minimizableItem = new QTreeWidgetItem(item);
    minimizableItem->setText(0, "Minimizable");
    minimizableItem->setText(1, window->isMinimizable() ? "✓" : "✗");
    item->addChild(minimizableItem);
    connect(window, &UkuiWindow::stateChanged, this, [minimizableItem, window](uint32_t state) {
        minimizableItem->setText(1, window->isMinimizable() ? "✓" : "✗");
    });

    QTreeWidgetItem *virtualDesktopChangeableItem = new QTreeWidgetItem(item);
    virtualDesktopChangeableItem->setText(0, "VirtualDesktopChangeable");
    virtualDesktopChangeableItem->setText(1, window->isVirtualDesktopChangeable() ? "✓" : "✗");
    item->addChild(virtualDesktopChangeableItem);
    connect(window, &UkuiWindow::stateChanged, this, [virtualDesktopChangeableItem, window](uint32_t state) {
        virtualDesktopChangeableItem->setText(1, window->isVirtualDesktopChangeable() ? "✓" : "✗");
    });

    QTreeWidgetItem *acceptFocusItem = new QTreeWidgetItem(item);
    acceptFocusItem->setText(0, "AcceptFocus");
    acceptFocusItem->setText(1, window->isAcceptFocus() ? "✓" : "✗");
    item->addChild(acceptFocusItem);
    connect(window, &UkuiWindow::stateChanged, this, [acceptFocusItem, window](uint32_t state) {
        acceptFocusItem->setText(1, window->isAcceptFocus() ? "✓" : "✗");
    });

    QTreeWidgetItem *skipTaskbarItem = new QTreeWidgetItem(item);
    skipTaskbarItem->setText(0, "SkipTaskbar");
    skipTaskbarItem->setText(1, window->isSkipTaskbar() ? "✓" : "✗");
    item->addChild(skipTaskbarItem);
    connect(window, &UkuiWindow::stateChanged, this, [skipTaskbarItem, window](uint32_t state) {
        skipTaskbarItem->setText(1, window->isSkipTaskbar() ? "✓" : "✗");
    });

    QTreeWidgetItem *skipSwitcherItem = new QTreeWidgetItem(item);
    skipSwitcherItem->setText(0, "SkipSwitcher");
    skipSwitcherItem->setText(1, window->isSkipSwitcher() ? "✓" : "✗");
    item->addChild(skipSwitcherItem);
    connect(window, &UkuiWindow::stateChanged, this, [skipSwitcherItem, window](uint32_t state) {
        skipSwitcherItem->setText(1, window->isSkipSwitcher() ? "✓" : "✗");
    });

    QTreeWidgetItem *modalityItem = new QTreeWidgetItem(item);
    modalityItem->setText(0, "Modality");
    modalityItem->setText(1, window->isModality() ? "✓" : "✗");
    item->addChild(modalityItem);
    connect(window, &UkuiWindow::stateChanged, this, [modalityItem, window](uint32_t state) {
        modalityItem->setText(1, window->isModality() ? "✓" : "✗");
    });

    m_treeWidget->addTopLevelItem(item);
}

void UkuiDebugConsole::handleWindowRemoved(UkuiWindow *window)
{
    QList<QTreeWidgetItem *> items = m_treeWidget->findItems(window->uuid(), Qt::MatchExactly);
    for (QTreeWidgetItem *item : items) {
        delete item;
    }
}

} // namespace ukuiwaylandclient
